package com.c611.smartclassroom.service.Impl;

import com.c611.smartclassroom.mapper.DeviceMapper;
import com.c611.smartclassroom.model.Device;
import com.c611.smartclassroom.model.DeviceExample;
import com.c611.smartclassroom.model.DeviceVo;
import com.c611.smartclassroom.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.apache.tomcat.util.IntrospectionUtils.setProperty;

@Service
public class DeviceServiceImpl implements DeviceService {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    DeviceMapper deviceMapper;
    //protected static final Logger logger = LoggerFactory.getLogger(DeviceServiceImpl.class);
    /**
     * 根据点击的教学楼，查询所有设备
     */
    @Override
    public List<Device> queryDevice(String buildId, String afGateway, Integer onlineStatus, Integer page, Integer limit) {
        page = (page-1)*limit;
        return deviceMapper.selectList(buildId,afGateway,onlineStatus,page,limit);
    }
    @Override
    public long queryDeviceCount(String buildId,String afGateway,Integer onlineStatus){
        return deviceMapper.selectListCount(buildId,afGateway,onlineStatus);
    }
    /**
     * 根据网关ID查询该网关下的所有设备
     * @param gateWayId
     * @return
     */
    public List<Device> queryDeviceByGateId(String gateWayId){
        DeviceExample deviceExample = new DeviceExample();
        DeviceExample.Criteria criteria = deviceExample.createCriteria();
        //查询是否置废
        criteria.andDelflagEqualTo(0);
        criteria.andAfgatewayEqualTo(gateWayId);
        return deviceMapper.selectByExample(deviceExample);
    }
    /**
     * 根据教室ID查询该教室的所有设备
     * @param classroomId
     * @return
     */
    @Override
    public List<Device> queryDeviceByClassId(String classroomId) {
        DeviceExample deviceExample = new DeviceExample();
        DeviceExample.Criteria criteria = deviceExample.createCriteria();
        criteria.andClassroomidEqualTo(classroomId);
        //查询是否置废
        criteria.andDelflagEqualTo(0);
        return deviceMapper.selectByExample(deviceExample);
    }
    @Override
    public long queryDevicesByTbuildIDClassroomIdCount(String buildId, String classroomId) {
        return deviceMapper.queryDevicesByTbuildIDClassroomIdCount(buildId,classroomId);
    }
    @Override
    public   List<DeviceVo> queryDevicesByTbuildIDClassroomId(String buildId, String classroomId, String controlMode){
        List<Device> devices = deviceMapper.queryDevicesByTbuildIDClassroomId(buildId,classroomId);
        List<DeviceVo> deviceVos = new ArrayList<DeviceVo>();
        //yw 增加从IoT返回的设备属性值 eqID,eqTypeID,eqType,onlStatus
        for(Device k:devices){
            DeviceVo deviceVo = new DeviceVo();

            deviceVo.setEqid(k.getEqid());
            deviceVo.setEqtypeid(k.getEqtypeid());
            deviceVo.setEqtypename(k.getEqtypename());
            deviceVo.setOnlstatus(k.getOnlstatus());

            switch(controlMode){
                case "classMode":
                    if(k.getEqtypename().equals("幕布")){
                        //设置属性值为1
                        setProperty(k.getEqid(),"PowerSwitch","1");
                        deviceVo.setPowerSwitch(1);
                        //logger.info(controlMode+controlMode);
                    }
                    if(k.getEqtypename().equals("空调")){
                        setProperty(k.getEqid(),"PowerSwitch","1");
                        deviceVo.setPowerSwitch(1);
                        //logger.info(controlMode);
                    }
                    break;
                case "finishClassMode":
                case "manualMode":
                    if(k.getEqtypename().equals("幕布")){
                        setProperty(k.getEqid(),"PowerSwitch","0");
                        deviceVo.setPowerSwitch(0);
                    }
                    if(k.getEqtypename().equals("空调")){
                        setProperty(k.getEqid(),"PowerSwitch","0");
                        deviceVo.setPowerSwitch(0);
                    }
                    break;
            }
            if(k.getEqtypename().equals("双键开关")){
                deviceVo.setPowerSwitch_1(1);
                deviceVo.setPowerSwitch_2(0);
            }
            if(k.getEqtypename().equals("三键开关")){
                deviceVo.setPowerSwitch_1(1);
                deviceVo.setPowerSwitch_2(0);
                deviceVo.setPowerSwitch_3(1);
            }

            deviceVos.add(deviceVo);
        }
        return deviceVos;
    }
    //    @Override
//    DeviceVo queryAirboxByTbuildIDClassroomId(String buildId, String classroomId, String controlMode){
//            Device device = deviceMapper.queryAirboxByTbuildIDClassroomId(buildId,classroomId);
//            DeviceVo deviceVo = new DeviceVo();
//            deviceVo.setEqid(device.getEqid());
//            deviceVo.setEqtypeid(device.getEqtypeid());
//            deviceVo.setEqtypename(device.getEqtypename());
//            deviceVo.setOnlstatus(device.getOnlstatus());
//
//            deviceVo.setCurrentTemperature(Math.floor((Math.random()*20)+10));
//            deviceVo.setCurrentHumidity((int)Math.floor((Math.random()*20)+60));
//            deviceVo.setPM25((int)Math.floor((Math.random()*50)+30));
//            deviceVo.setCO2((int)Math.floor((Math.random()*600)+300));
//            DecimalFormat df = new DecimalFormat("0.000");
//            deviceVo.setHCHO(Double.parseDouble(df.format(Math.random())));
//            return deviceVo;
//    }
    @Override
    public long queryDevicesByAfgatewayCount(String afgateway){
        return deviceMapper.queryDevicesByAfgatewayCount(afgateway);
    }
    @Override
    public List<DeviceVo> queryDevicesByAfgateway(String afgateway){
        List<Device> devices = deviceMapper.queryDevicesByAfgateway(afgateway);
        List<DeviceVo> deviceVos = new ArrayList<DeviceVo>();
        //yw 增加从IoT返回的设备属性值 eqID,eqTypeID,eqType,onlStatus
        for(Device k:devices){
            DeviceVo deviceVo = new DeviceVo();  //yw向下转型
            //deviceVo.setAddtime(k.getAddtime());
            //deviceVo.setAfgateway(k.getAfgateway());
            deviceVo.setEqid(k.getEqid());
            deviceVo.setEqtypeid(k.getEqtypeid());
            deviceVo.setEqtypename(k.getEqtypename());
            deviceVo.setOnlstatus(k.getOnlstatus());
//            deviceVo.setPowerSwitch_1(1);
//            deviceVo.setPowerSwitch_2(0);
//            deviceVo.setPowerSwitch_3(1);
//            deviceVo.setPowerSwitch(1);
//            deviceVo.setPauseSwitch(1);
            if(k.getEqtypename().equals("窗帘")){
                deviceVo.setPowerSwitch(1);
                deviceVo.setPauseSwitch(1);
            }
            else if(k.getEqtypename().equals("双键开关")){
                deviceVo.setPowerSwitch_1(1);
                deviceVo.setPowerSwitch_2(0);
            }
            else if(k.getEqtypename().equals("三键开关")){
                deviceVo.setPowerSwitch_1(1);
                deviceVo.setPowerSwitch_2(0);
                deviceVo.setPowerSwitch_3(1);
            }
            else{
                deviceVo.setPowerSwitch(0);
            }
            deviceVos.add(deviceVo);
        }
        return deviceVos;
    }
    /**
     * 根据设备ID查询该设备的所有参数
     * @param eqId
     * @return
     */
    @Override
    public Device queryDataByDeviceId(String eqId) {
        Device device = deviceMapper.selectByPrimaryKey(eqId);
        if(device.getDelflag() != 1){
            return device;
        }
        return null;
    }

    /**
     * 增加设备
     * @param device
     * @return
     */
    @Override
    public int addDevice(Device device) {
        //设置添加时的时间
        device.setAddtime(new Date());
        return deviceMapper.insertSelective(device);
    }

    /**
     * 编辑设备信息
     * @param device
     * @return
     */
    @Override
    public int saveDevice(Device device) {
        return deviceMapper.updateByPrimaryKeySelective(device);
    }

    /**
     * 删除设备
     * @param eqId
     * @return
     */
    @Override
    public int delDevice(String eqId){
        return deviceMapper.deleteByPrimaryKey(eqId);
    }
}
